package com.xiaobaibai.units;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Last7DaysUtil {

//    private Date getDateAdd(int days){
//        Calendar c = Calendar.getInstance();
//        //按照日历的规则，给指定字段添加或减少时间量。
//        c.add(Calendar.DAY_OF_MONTH, -days);
//        return c.getTime();
//    }
//    //网上的方法
//    private  List<String> getDaysBetwwen(int days){
//        List<String> dayss = new ArrayList<>();
//        Calendar start = Calendar.getInstance();
//        start.setTime(getDateAdd(days));
//        Long startTIme = start.getTimeInMillis();
//        Calendar end = Calendar.getInstance();
//        end.setTime(new Date());
//        Long endTime = end.getTimeInMillis();
//        Long oneDay = 1000 * 60 * 60 * 24l;
//        Long time = startTIme;
//        while (time <= endTime) {
//            Date d = new Date(time);
//            DateFormat df = new SimpleDateFormat("MMdd");
//            dayss.add(df.format(d));
//            time += oneDay;
//        }
//        return dayss;
//    }

    //启发自己写的
    public static List<String> getRecent7Day(){
        List<String> list=new ArrayList<>();
        Date date=new Date();
        for(int i=6;i>=0;i--){
            Date d = DateUtils.addDays(date, -i);
            String mMdd = DateFormatUtils.format(d, "MM/dd");
            list.add(mMdd);
        }
        return list;
    }



    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(getRecent7Day()));
    }

}
